package com.fdb.efp.flow.service.impl;

import java.util.List;
import java.util.Objects;

import com.fdb.efp.flow.service.dao.BizFlowProcessDao;
import com.fdb.efp.flow.service.vo.BizFlowProcessVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fdb.efp.flow.service.domain.BizFlowProcess;
import com.fdb.efp.flow.service.facade.BizFlowProcessService;
import com.fdb.basic.framework.core.base.FrameworkService;
/**
 * <p>ServiceImpl: 业务流转过程配置				<p>
 * @author ligm
 * @date 2018-05-05
 */
@Service("bizFlowProcessService")
public class BizFlowProcessServiceImpl implements BizFlowProcessService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(BizFlowProcessServiceImpl.class);

	@Autowired
	private BizFlowProcessDao bizFlowProcessDao;
	
	/**
	 * 新增操作
	 */
	public int insertBizFlowProcess(BizFlowProcessVO inBizFlowProcessVo) {
		logger.debug("当前新增数据为:"+ inBizFlowProcessVo.toString());
		int num = 0;
		try {
			BizFlowProcess bizFlowProcess = new BizFlowProcess();
			beanCopy(inBizFlowProcessVo, bizFlowProcess);
			num = bizFlowProcessDao.insertBizFlowProcess(bizFlowProcess);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(BizFlowProcessVO inBizFlowProcessVo) {
		logger.debug("当前删除数据条件为:"+ inBizFlowProcessVo);
		int num = 0;
		try {
			BizFlowProcess bizFlowProcess = new BizFlowProcess();
			beanCopy(inBizFlowProcessVo, bizFlowProcess);
			num = bizFlowProcessDao.deleteByPk(bizFlowProcess);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizFlowProcessVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(BizFlowProcessVO inBizFlowProcessVo) {
		logger.debug("当前修改数据为:"+ inBizFlowProcessVo.toString());
		int num = 0;
		try {
			BizFlowProcess bizFlowProcess = new BizFlowProcess();
			beanCopy(inBizFlowProcessVo, bizFlowProcess);
			num = bizFlowProcessDao.updateByPk(bizFlowProcess);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inBizFlowProcessVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public BizFlowProcessVO queryByPk(BizFlowProcessVO inBizFlowProcessVo) {
		
		logger.debug("当前查询参数信息为:"+ inBizFlowProcessVo);
		try {
			BizFlowProcess queryBizFlowProcess = new BizFlowProcess();
			beanCopy(inBizFlowProcessVo, queryBizFlowProcess);
			BizFlowProcess queryRslBizFlowProcess = bizFlowProcessDao.queryByPk(queryBizFlowProcess);
			if (Objects.nonNull(queryRslBizFlowProcess)) {
				BizFlowProcessVO outBizFlowProcessVo = beanCopy(queryRslBizFlowProcess, new BizFlowProcessVO());
				logger.debug("当前查询结果为:"+ outBizFlowProcessVo.toString());
				return outBizFlowProcessVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizFlowProcessVO> queryAllOwner(BizFlowProcessVO bizFlowProcessVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<BizFlowProcessVO> list = null;
		try {
			List<BizFlowProcess> bizFlowProcesss = bizFlowProcessDao.queryAllOwnerByPage(bizFlowProcessVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ bizFlowProcesss.size());
			pageSet(bizFlowProcesss, bizFlowProcessVo);
			list = (List<BizFlowProcessVO>) beansCopy(bizFlowProcesss, BizFlowProcessVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizFlowProcessVO> queryAllCurrOrg(BizFlowProcessVO bizFlowProcessVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<BizFlowProcess> bizFlowProcesss = bizFlowProcessDao.queryAllCurrOrgByPage(bizFlowProcessVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+bizFlowProcesss.size());
		List<BizFlowProcessVO> list = null;
		try {
			pageSet(bizFlowProcesss, bizFlowProcessVo);
			list = (List<BizFlowProcessVO>) beansCopy(bizFlowProcesss, BizFlowProcessVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<BizFlowProcessVO> queryAllCurrDownOrg(BizFlowProcessVO bizFlowProcessVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<BizFlowProcess> bizFlowProcesss = bizFlowProcessDao.queryAllCurrDownOrgByPage(bizFlowProcessVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ bizFlowProcesss.size());
		List<BizFlowProcessVO> list = null;
		try {
			pageSet(bizFlowProcesss, bizFlowProcessVo);
			list = (List<BizFlowProcessVO>) beansCopy(bizFlowProcesss, BizFlowProcessVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}

	@Override
	public BizFlowProcessVO queryFirstByEventAndNodeId(BizFlowProcessVO bizFlowProcess) {
		logger.debug("根据事件ID："+ bizFlowProcess.getBizEventId() +",节点ID："+ bizFlowProcess.getBizNodeId() +"查询第一条数据开始......");
		try {
			BizFlowProcess queryBizFlowProcess = new BizFlowProcess();
			beanCopy(bizFlowProcess, queryBizFlowProcess);
			BizFlowProcess queryRslBizFlowProcess = bizFlowProcessDao.queryFirstByEventAndNodeId(queryBizFlowProcess);
			if (Objects.nonNull(queryRslBizFlowProcess)) {
				BizFlowProcessVO outBizFlowProcessVo = beanCopy(queryRslBizFlowProcess, new BizFlowProcessVO());
				logger.debug("根据事件ID："+ bizFlowProcess.getBizEventId() +",节点ID："+ bizFlowProcess.getBizNodeId() +"查询第一条数据结束......");
				return outBizFlowProcessVo;
			} else {
				logger.warn("根据事件ID："+ bizFlowProcess.getBizEventId() +",节点ID："+ bizFlowProcess.getBizNodeId() +"查询第一条数据结果为空!");
			}
		} catch (Exception e) {
			logger.error("根据事件ID："+ bizFlowProcess.getBizEventId() +",节点ID："+ bizFlowProcess.getBizNodeId() +"查询第一条数据出现异常!", e);
		}
		return null;
	}
	
	
	public BizFlowProcessVO queryDefaultByEventAndFlowId(BizFlowProcessVO bizFlowProcess) {

		logger.debug("根据事件ID："+ bizFlowProcess.getBizEventId() +",流程ID："+ bizFlowProcess.getBizFlowId() +"查询默认的第一条数据开始......");
		try {
			BizFlowProcess queryBizFlowProcess = new BizFlowProcess();
			beanCopy(bizFlowProcess, queryBizFlowProcess);
			BizFlowProcess queryRslBizFlowProcess = bizFlowProcessDao.queryDefaultByEventAndFlowId(queryBizFlowProcess);
			if (Objects.nonNull(queryRslBizFlowProcess)) {
				BizFlowProcessVO outBizFlowProcessVo = beanCopy(queryRslBizFlowProcess, new BizFlowProcessVO());
				logger.debug("根据事件ID："+ bizFlowProcess.getBizEventId() +",流程ID："+ bizFlowProcess.getBizFlowId() +"查询默认的第一条数据结束......");
				return outBizFlowProcessVo;
			} else {
				logger.warn("根据事件ID："+ bizFlowProcess.getBizEventId() +",流程ID："+ bizFlowProcess.getBizFlowId() +"查询默认的第一条数据结果为空!");
			}
		} catch (Exception e) {
			logger.error("根据事件ID："+ bizFlowProcess.getBizEventId() +",流程ID："+ bizFlowProcess.getBizFlowId() +"查询默认的第一条数据出现异常!", e);
		}
		return null;
	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BizFlowProcessVO> queryAllByEventIdAndFlowId(BizFlowProcessVO bizFlowProcess) {
		logger.debug("根据事件ID："+ bizFlowProcess.getBizEventId() +",节点ID："+ bizFlowProcess.getBizNodeId() +"查询所有节点数据开始......");
		List<BizFlowProcessVO> list = null;
		try {
			BizFlowProcess queryBizFlowProcess = new BizFlowProcess();
			beanCopy(bizFlowProcess, queryBizFlowProcess);
			List<BizFlowProcess> processList = bizFlowProcessDao.queryAllByEventIdAndFlowId(queryBizFlowProcess);
			list = (List<BizFlowProcessVO>) beansCopy(processList, BizFlowProcessVO.class);
		} catch (Exception e) {
			logger.error("根据事件ID："+ bizFlowProcess.getBizEventId() +",节点ID："+ bizFlowProcess.getBizNodeId() +"查询所有节点数据出现异常!", e);
		}
		return list;
	}
	
}
